package com.fzebu.fastboot.form.parser.impl;

import com.fzebu.fastboot.annotations.form.Select;
import com.fzebu.fastboot.form.parser.AbstractTypeParser;

import java.lang.reflect.Field;

/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/1/26 22:50
 */
public class SelectTypeParser extends AbstractTypeParser {

    public SelectTypeParser(Field field, String alias, String value) {
        super(field, alias, value);
    }

    @Override
    public Object parse() {
        Select select = this.field.getAnnotation(Select.class);
        for (String item : select.value()) {
            int pos = item.indexOf('-');
            if (pos == -1) {
                break;
            }
            String id = item.substring(0, pos);
            String name = item.substring(pos + 1);
            if (id.equals(value)) {
                return name;
            }
        }
        return value;
    }
}
